package file

import (
	"context"
	"fmt"

	"mall/common/utils/pointy"
	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/backend/api/internal/logic/media/util"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/i18n"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetFileListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetFileListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFileListLogic {
	return &GetFileListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetFileListLogic) GetFileList(req *types.MediaFileListReq) (resp *types.MediaFileListResp, err error) {
	// logrus.Info(fmt.Sprintf("MediaFileListReq: %+v", req))

	page, pageSize := parse.ParsePageInfo(
		req.Current,
		req.PageSize,
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)

	orderBys := util.ParseSorter(req.Sorter)
	filterBys := util.ParseFilter(req.Filter)

	var ids *string
	// if req.Id != nil && *req.Id > 0 {
	// 	ids = pointy.GetPointer(strconv.FormatInt(int64(*req.Id), 10))
	// }

	data, err := l.svcCtx.AttachmentRpc.BackendGetFileList(l.ctx, &attachmentclient.FileListReq{
		Page:     page,
		PageSize: pageSize,
		Ids:      ids,
		ParentId: req.ParentId,
		// UserId:   req.UserId,
		FileName: req.FileName,
		AltText:  req.AltText,
		Type:     req.FileType,
		Status:   req.Status,
		Search:   req.Search,
		// StartTime: req.StartTime,
		// EndTime:   req.EndTime,
		OrderBy:  orderBys,
		FilterBy: filterBys,
	})
	if err != nil {
		return
	}

	resp = &types.MediaFileListResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: types.MediaFileListInfo{
			BaseListInfo: types.BaseListInfo{
				Current:  page,
				PageSize: pageSize,
				Total:    data.GetTotal(),
			},
		},
	}
	for _, v := range data.List {
		option := util.GetOptionByStatusAndType(v.Status, v.Type)

		var url *string
		if v.Url != nil && len(*v.Url) > 0 {
			url = pointy.GetPointer(fmt.Sprintf("%s%s", l.svcCtx.Config.NfsConf.StaticUrl, *v.Url))
		}
		var uri *string
		if v.Path != nil && len(*v.Path) > 0 {
			uri = pointy.GetPointer(fmt.Sprintf("%s%s", l.svcCtx.Config.NfsConf.ProxyUrl, *v.Path))
		}
		if url == nil || len(*url) == 0 {
			url = uri
		}

		resp.Data.List = append(resp.Data.List, &types.MediaFileInfo{
			BaseUUIDInfo: types.BaseUUIDInfo{
				Id:         v.Id,
				CreatedAt:  v.CreatedAt,
				UpdatedAt:  v.UpdatedAt,
				DeletedAt:  v.DeletedAt,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			Filename: v.Filename,
			// Name:     v.Filename,
			Path:     uri,
			Url:      url,
			FileType: v.Type,
			MimeType: v.MimeType,
			Size:     v.Size,
			Md5:      v.Md5,
			ParentId: v.ParentId,
			UserId:   v.UserId,
			AltText:  v.AltText,
			// Title:         v.AltText,
			Excerpt:       v.Excerpt,
			Status:        v.Status,
			Sort:          v.Sort,
			Password:      v.Password,
			CommentStatus: v.CommentStatus,
			CommentCount:  v.CommentCount,
			// Count:         v.Count,
			CanPut:       option.CanPut,
			CanWatermark: option.CanWatermark,
			CanThumbnail: option.CanThumbnail,
		})
	}

	return resp, nil
}
